﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Edm;
using Obsidian.Action;
using Oak.Core;
using Oak.Model;
using Oak.Base;
using Oak.Base.Logic;

namespace Oak.Base.Serv
{
    public class AdminServ : OakServ<ISession>
    {
        private const string FN_ADMIN = "adminId,name,email,mobileNo,modified,created,status";
        private const string FN_LISTATTR = "itemsCount,pageNo,pageSize,pagesCount";

        public AdminServ() : base() { }

        public AdminServ(ISession session) : base(session) { }

        public void Get(ActionRequest req, ActionResponse res)
        {
            if(!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }
            string msg; 
            AdminLG adminLG = new AdminLG(Session);
            BsAdminInfo admin = adminLG.GetAdminById(Session.AccountId, out msg);
            if (admin == null)
            {
                res.Error(msg);
                return;
            }

            res.AddResult("admins", FN_ADMIN, admin);

        }

        public void Update(ActionRequest req, ActionResponse res)
        {
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }

            BsAdminInfo admin = req.GetModel<BsAdminInfo>("admin");

            string msg;
            AdminLG adminLG = new AdminLG(Session);
            admin = adminLG.Update(admin, out msg);
            if (admin == null)
            {
                res.Error(msg);
                return;
            }

            res.AddResult("admins", FN_ADMIN, admin);

        }


        public void Edit(ActionRequest req, ActionResponse res)
        {
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }

            BsAdminInfo admin = req.GetModel<BsAdminInfo>("admin");

            string msg;
            AdminLG adminLG = new AdminLG(Session);
            admin = adminLG.Edit(admin, out msg);
            if (admin == null)
            {
                res.Error(msg);
                return;
            }

            res.AddResult("admins", FN_ADMIN, admin);

        }

        public void List(ActionRequest req, ActionResponse res)
        {
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }

            BsAdminQry qry = req.GetModelByNameOrFirst<BsAdminQry>("qry");
            AdminLG adminLG = new AdminLG(Session);
            string msg;
            ListAttrInfo listAttr;
            IList<BsAdminInfo> list = adminLG.GetAdminList(qry, out listAttr, out msg);
            if (list == null)
            {
                res.Error(msg);
                return;
            }

            res.AddResult("admins", FN_ADMIN, list);
            res.AddResult("listAttr", FN_LISTATTR, listAttr);
        }

        public void ById(ActionRequest req, ActionResponse res)
        {
            //判断登录
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }

            ActReqParam prmAdmin = req.GetParamByNameOrFirst("admin");
            long adminId;
            if (!prmAdmin.TryGetLong(0, "adminId", out adminId))
            {
                res.Error("参数adminId错误");
                return;
            }

            string msg;
            AdminLG adminLG = new AdminLG(Session);
            BsAdminInfo admin = adminLG.GetAdminById(adminId, out msg);
            res.AddResult("admins", FN_ADMIN, admin);

        }

    }
}
